package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.Role;
import com.cskaoyan.bean.bo.AdminRoleVO;
import com.cskaoyan.bean.bo.PermissionBO;
import com.cskaoyan.bean.vo.PermissionFirstChildVO;
import com.cskaoyan.service.AdminRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author heyyon
 * 系统管理-管理员
 */
@RestController
@RequestMapping("admin/role")
public class AdminRoleController {
    @Autowired
    AdminRoleService adminRoleService;
    @RequestMapping("options")
    public BaseRespVo options() {
        List<AdminRoleVO> adminRoleList = adminRoleService.options();
        return BaseRespVo.ok(adminRoleList);
    }

    @RequestMapping("list")
    public BaseRespVo list(Integer page, Integer limit, String name, String sort, String order) {
        ListData<Role> roleList = adminRoleService.list(page, limit, name, sort, order);
        return BaseRespVo.ok(roleList);
    }

    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Role role) {
        Role insertedRole = adminRoleService.create(role);
        return BaseRespVo.ok(insertedRole);
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Role role) {
        int row = adminRoleService.update(role);
        if (row != 0) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Role role) {
        int i = adminRoleService.delete(role);
        if (i == -1) {
            return BaseRespVo.fail("该角色下有管理员，不能进行删除");
        }
        if (i == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }
    //授权管理
    @GetMapping("permissions")
    public BaseRespVo permissions(Integer roleId) {
        PermissionFirstChildVO permissionFirstChildVO=adminRoleService.permissions(roleId);
        return BaseRespVo.ok(permissionFirstChildVO);
    }

    //
    @PostMapping("permissions")
    public BaseRespVo permissions(@RequestBody PermissionBO permissionBO) {
        int i=adminRoleService.permissions(permissionBO);
        return BaseRespVo.ok();
    }
}
